﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Speech.Recognition;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace SimpleSpeechRecog
{
	public partial class Form1 : Form
	{
        //Speech recognition-related declarations
		SpeechRecognizer speechReco = new SpeechRecognizer();

        //Network-related declarations
        private UdpClient client;
        private IPEndPoint receivePoint;
        private int port = 6061; //Port for the Client to use
        private int ip = 127001;//IP Address 127.0.0.1

		public Form1()
		{
			InitializeComponent();

            //Init network variables
            client = new UdpClient(port);
            receivePoint = new IPEndPoint(new IPAddress(ip), port);

            //Init speech recog
            loadStartupGrammar();
			speechReco.Enabled = true;

			//Listen to Speech Recognizer's events
			speechReco.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(speechReco_SpeechRecognized);
			speechReco.SpeechDetected += new EventHandler<SpeechDetectedEventArgs>(speechReco_SpeechDetected);
        }

        void sendData(String data)
        {
            //Send DataGram
            //Format: Hostname@Post@Message
            System.Text.ASCIIEncoding encode = new System.Text.ASCIIEncoding();
            string sendString = "localhost@" + port.ToString() + "@" + data;
            byte[] sendData = encode.GetBytes(sendString);
            //Send to Server 
            client.Send(sendData, sendData.Length, "192.168.1.2", 6767);
            //client.Send(sendData, sendData.Length, "localhost", 6767);

        }

		void speechReco_SpeechDetected(object sender, SpeechDetectedEventArgs e)
		{
		}

		void speechReco_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
		{
            recognizedTextBox.AppendText("> " + e.Result.Text + "\n");
            sendData(e.Result.Text);    //Send it to Lisa
		}

        void stop()
        {

        }

        private void gram1Button_Click(object sender, EventArgs e)
        {
            loadGrammar1();
        }

        void loadStartupGrammar()
        {
            speechReco.UnloadAllGrammars();
            //Build grammar list
            List<String> grammarList = new List<String>();
            grammarList.Add("Lisa Visitor");
            grammarList.Add("Lisa Music");
            grammarList.Add("Lisa Television");
            grammarList.Add("Lisa Kitchen");

            /*grammarList.Add("Guten Tag");
            grammarList.Add("Lisa Musik");
            grammarList.Add("Radio ein");
            grammarList.Add("Radio aus");
            grammarList.Add("Lisa Küche");
            grammarList.Add("Backofen ein");
            grammarList.Add("Backofen aus");
            grammarList.Add("Danke Lisa");*/

            //Add grammar to Speech Recognizer
            Choices myChoices = new Choices(grammarList.ToArray());
            GrammarBuilder builder = new GrammarBuilder(myChoices);
            //builder.Culture = "en-US";
           // builder.Culture = new System.Globalization.CultureInfo("en-US");
            //builder.Culture = new System.Globalization.CultureInfo("de-DE");
            Grammar gram = new Grammar(builder);

            speechReco.LoadGrammar(gram);
        }

        void loadGrammar1()
        {
            speechReco.UnloadAllGrammars();
            List<String> grammarList = new List<String>();
            grammarList.Add("Grammar One");
   
			//Add grammar to Speech Recognizer
			Choices myChoices = new Choices(grammarList.ToArray());
			GrammarBuilder builder = new GrammarBuilder(myChoices);
			//builder.Culture = new System.Globalization.CultureInfo("en-US");
            //builder.Culture = new System.Globalization.CultureInfo("de-DE");
			Grammar gram = new Grammar(builder);

			speechReco.LoadGrammar(gram);
        }

        private void gram2Button_Click(object sender, EventArgs e)
        {
            speechReco.UnloadAllGrammars();
            List<String> grammarList = new List<String>();
            grammarList.Add("Grammar Two");

            //Add grammar to Speech Recognizer
            Choices myChoices = new Choices(grammarList.ToArray());
            GrammarBuilder builder = new GrammarBuilder(myChoices);
            //builder.Culture = new System.Globalization.CultureInfo("en-US");
            //builder.Culture = new System.Globalization.CultureInfo("de-DE");
            Grammar gram = new Grammar(builder);

            speechReco.LoadGrammar(gram);
        }
	}
}
